using System.Text.Json.Serialization;

namespace hao_Common.Response.Models
{
    /// <summary>
    /// 统一API响应模型（泛型版本） 
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    // 泛型版本
    public class ApiResponse<T>
    {
        /// <summary>
        /// 状态码：200=成功，400=客户端错误，401=未授权，403=禁止访问，404=未找到，500=服务器错误
        /// </summary>
        public int Code { get; set; }
        public string Message { get; set; } = string.Empty;
        public T? Data { get; set; }
        public string? ErrorCode { get; set; }
        public DateTime Timestamp { get; set; } = DateTime.UtcNow;
        public string RequestId { get; set; } = Guid.NewGuid().ToString();
        
        /// <summary>
        /// 是否成功（只读属性，基于状态码判断）
        /// </summary>
        [JsonIgnore]
        public bool Success => Code >= 200 && Code < 300;
    }

    // 非泛型版本
    public class ApiResponse : ApiResponse<object>
    {
    }

    /// <summary>
    /// 分页响应模型
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class PagedApiResponse<T> : ApiResponse<IEnumerable<T>>
    {
        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageNumber { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages => (int)Math.Ceiling((double)TotalCount / PageSize);

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage => PageNumber > 1;

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage => PageNumber < TotalPages;
    }
}